﻿using System;
using System.Linq;
using Lucene.Linq.Azure;
using Lucene.Linq.Storage;
using Lucene.Net.Store;
using Microsoft.WindowsAzure;

namespace Lucene.Linq.Demo3_Azure
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This demo requires the Azure Storage Emulator to be running");
            Console.WriteLine("You can also use an actual Azure Storage Account, modify the app.config");
            PressAnyKeyToContinue();

            CloudStorageAccount account = AccountHelper.Account("Lucene.Linq.Demo3_Azure.Properties.Settings.AzureConnectionString", false);

            // Lucene Index Stored in Azure Blob Storage
            Directory azureBlobDirectory = new AzureDirectory(account);
            StorageManager managerdemo1 = new StorageManager(azureBlobDirectory);
            DemoPerson(managerdemo1, account);
            DemoJob(managerdemo1, account);

            // Lucene Index Stored in Azure Cloud Drive
            // This does not work because it requires a DriveLocalCache which is only available to an AzureRole
            //Directory azureDriveDirectory = new AzureDriveDirectory(account, "vhd\test.vhd");
            //StorageManager managerdemo2 = new StorageManager(azureDriveDirectory);
            //DemoPerson(managerdemo2, account);
            //DemoJob(managerdemo2, account);

            Console.WriteLine("This concludes the demo of Azure Indexed Blob/Table");
            PressAnyKeyToContinue();
        }

        private static void PressAnyKeyToContinue()
        {
            Console.WriteLine("Press the ANY key to continue");
            Console.ReadKey();
        }

        private static void DemoPerson(StorageManager manager, CloudStorageAccount account)
        {
            Console.WriteLine("Add index type");
            manager.Add<Person>(new AzureBlobEntityStorage<Person>(account, "person"));

            manager.Add(new Person("1", "Bill"));

            manager.Add(new Person("2", "Sam"));

            // We should get only 2 records here
            foreach (Person person in manager.Search<Person>().Where(p => p.Id == "1"))
            {
                Console.WriteLine("{0} {1}", person.Id, person.Name);
            }

            foreach (string s in manager.GetAll(typeof(Person)))
            {
                Console.WriteLine("{0}", s);
            }

            manager.Remove(typeof(Person), "person1");

            manager.Remove(typeof(Person), "person2");
        }

        private static void DemoJob(StorageManager manager, CloudStorageAccount account)
        {
            manager.Add<Job>(new AzureTableEntityStorage<Job>(account, "job"));

            manager.Add(new Job("1", "Sell"));
            manager.Add(new Job("2", "Buy"));

            foreach (Job job in manager.Search<Job>().Where(p => p.Id == "1"))
            {
                Console.WriteLine("{0} {1}", job.Id, job.Name);
            }

            foreach (string s in manager.GetAll(typeof(Job)))
            {
                Console.WriteLine("{0}", s);
            }

            manager.Remove(typeof(Job), "job1");
            manager.Remove(typeof(Job), "job2");
            Console.ReadKey();
        }
    }
}
